const router = require("koa-router")();
const MembersModel = require("../../models/Members")
// 查询数据库
const upload = require("../../config/multer")

router.post('/m1/doEdit',upload.single('file'), async (ctx, next) => {
    console.log(ctx.req.body)
    // console.log(ctx.req.file.filename)
    var {id,name,age,like,friend} = ctx.req.body;
    /*必须上传图片
    var avatar = ctx.req.file.filename;
    var host = ctx.request.header.host;
    avatar = `http://${host}/${avatar}`
    await MembersModel.updateOne({
        _id: id
    }, {
        name,
        age:Number(age),
        like:Boolean(like),
        friend:Boolean(friend),
        avatar
    }) */
//不选择上传图片时选用原图片
        if(ctx.req.file == undefined){
            await MembersModel.updateMany(
                {_id:id},
                {
                name,
                age,
                like:Boolean(like),
                friend:Boolean(friend),
            });
        }else{
            var avatar = ctx.req.file.filename;
            var host = ctx.request.header.host;
            avatar = `http://${host}/${avatar}`;
            await MembersModel.updateMany(
                {_id:id},
                {
                name,
                age,
                like:Boolean(like),
                friend:Boolean(friend),
                avatar
            });
        }
    
    ctx.redirect("/m1")
}) 
module.exports = router;